<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column label="订单号" align="center" prop="orderNum" width="95">
        <template slot-scope="scope">
          <el-tag type="info" size="small">{{ scope.row.orderNum }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createAt" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createAt, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateAt" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateAt, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户姓名" align="center" prop="customName"/>
      <el-table-column label="产品名称" align="center" prop="proName">
        <template slot-scope="scope">
          <el-tag type="success" size="small">{{ scope.row.proName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="产品数量" align="center" prop="proQuantity"/>
      <el-table-column label="计划工时" align="center" prop="planHours" >
        <template slot-scope="scope">
          <el-tag type="primary" size="small">{{ scope.row.planHours }}工时</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="finishDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排产状态" align="center" prop="productionSchedule">
        <template slot-scope="scope">
          <span v-if="scope.row.productionSchedule === '0'">
              <el-tag type="danger" plain size="small">未排产</el-tag>
          </span>
          <span v-else-if="scope.row.productionSchedule === '1'">
              <el-tag type="success" size="small">已排产</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="生产状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.orderStatus === '0'">
              <el-tag type="danger" size="small">未完成</el-tag>
          </span>
          <span v-else-if="scope.row.orderStatus === '1'">
              <el-tag type="success" size="small">已完成</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:order:edit']"
          >排产
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:order:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 订单排产对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="排产对象" prop="proQuantity">
          <el-select v-model="form.retailList" placeholder="请选择排产对象">
            <el-option
              v-for="r in retailList"
              :key="r.rId"
              :label="r.rContact"
              :value="r.rId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="排产数量" prop="proQuantity">
          <el-input v-model="form.proQuantity" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="计划工时" prop="planHours">
          <el-input v-model="form.planHours" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="结束日期" prop="finishDate">
          <el-date-picker
            v-model="form.finishDate"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOrder, getOrder, delOrder, addOrder, updateOrder, WatchRetail} from "@/api/system/order";
import {getRetail} from "@/api/system/retail";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 供应商信息数据
      retailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNum: null,
        createAt: null,
        updateAt: null,
        customId: null,
        proId: null,
        proQuantity: null,
        productionSchedule: null,
        orderStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        proQuantity: [
          { required: true, message: "排产数量不能为空", trigger: "blur" }
        ],
        retailList: [
          { required: true, message: "排产对象不能为空", trigger: "change" }
        ],
        planHours: [
          { required: true, message: "计划工时不能为空", trigger: "blur" }
        ],
        finishDate: [
          { required: true, message: "结束日期不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderNum: null,
        createAt: null,
        updateAt: null,
        customId: null,
        proId: null,
        proQuantity: null,
        productionSchedule: null,
        orderStatus: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "订单排产";
      getOrder(row.orderId).then(response => {
        this.form = response.data;
      });
      WatchRetail(row.proId).then(response => {
        this.retailList = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateOrder(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            console.log(this.form);
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      const orderNum = row.orderNum || this.ids;
      this.$modal.confirm('是否确认删除订单号为"' + orderNum + '"的数据项？').then(function () {
        return delOrder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
